package com.buba.manager.controller;

import com.buba.manager.pojo.Book;
import com.buba.manager.pojo.Page;
import com.buba.manager.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 图书控制层
 */
@RestController//SpringBoot中提供的注解  是@Controller和@ResponseBody的组合
@RequestMapping("/Book")
public class BookController {
    private BookService bookService;

    @RequestMapping("/getBook")
    public Map<String , Object> getBook(){

        Map<String , Object> map = new HashMap<>();
        map.put("1","温智的快乐一夜");
        map.put("2","狗佳的放纵生活");
        return map;
    }

    /**
     * 修改图书方法
     * @param book
     * @return
     */
    @RequestMapping("/updBook")
    public Map<String , Object> updBook(Book book){
        System.out.println(book);
        Map<String , Object> map = new HashMap<>();
        Boolean b = bookService.updBook(book);
        if(b==true)
            map.put("msg","修改成功！");
        map.put("msg","失败！");
        return map;
    }

    /**
     * 添加图书方法
     * @return
     */
    @RequestMapping("/addBook")
    public Map<String , Object> addBook(Book book){
        Map<String , Object> map = new HashMap<>();
        //把要添加的信息传入到业务层
        Integer count=bookService.addBook(book);
        if (count==1){
            map.put("msg","添加成功！");

        }else {
            map.put("msg","添加失败！");
        }
        return map;
    }


    /**
     * 查询图书方法
     * @return
     */
    @RequestMapping("/selBook")
    public Map<String , Object> selBook(Integer pageNow,Integer pageCount){
        Map<String , Object> map = new HashMap<>();
        Page page = new Page(pageNow,pageCount);
        //查询总条数
        long count = bookService.selCount();
        map.put("count",count);
        page.computeTotal(count);
        map.put("page",page);
        //调用业务层 用list集合接收
        List<Book> list = bookService.selBook(map);
        //System.out.println(page+limit);
        map.put("data",list);
        //总页数根据显示的页数进行计算

        return map;
    }

    /**
     * 删除图书方法
     * @return
     */
    @RequestMapping("/delBook")
    public Map<String , Object> delBook(String bookIds){
        String[] split = bookIds.split(",");
        List<String> list = Arrays.asList(split);
        Map<String , Object> map = new HashMap<>();
        //调用业务层 用list集合接收
        Integer count=bookService.delBook(list);
        map.put("code",0);
        if(count > 0){
            map.put("msg","删除了"+count+"条数据");
        }else{
            map.put("msg","删除失败");
        }

        return map;
    }




    public BookService getBookService() {
        return bookService;
    }
    @Autowired
    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }
}
